﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace POB.Gameplay.Components
{
    class Hull : Component, IColideable
    {
        List<Circle> collideCircles = new List<Circle>();

        public Hull(XElement unitElement, World world, Player player, Unit unit)
            :base(unitElement, world, player, unit)
        {
            collideCircles.AddRange(unitElement.Elements("Circle")
                .Select(e => new Circle()
                {
                    position = e.Attribute("Position").Value.ToVector2(),
                    radius = Convert.ToSingle(e.Attribute("Radius").Value) 
                }));

            world.Colliders.Add(this);
        }
        
        public override void Update(float time)
        {
            base.Update(time);
        }

        public override void Draw(float time, Microsoft.Xna.Framework.Graphics.SpriteBatch spriteBatch, Microsoft.Xna.Framework.Matrix matrix)
        {
            spriteBatch.Begin(matrix);
            Random r = new Random(1);

            foreach(var circle in collideCircles)
            {
                spriteBatch.Draw(World.Content.Load<Texture2D>("blank"),
                    new Rectangle((int)(circle.position.X - circle.radius / 2), (int)(circle.position.Y - circle.radius / 2), (int)(circle.radius), (int)(circle.radius)),
                    new Color((float)r.NextDouble() / 2, (float)r.NextDouble() / 2, (float)r.NextDouble() / 2, .5f));
            }

            spriteBatch.End();
            base.Draw(time, spriteBatch, matrix);
        }

        public IEnumerable<Circle> Circles
        {
            get { return collideCircles; }
        }

        public Component Component
        {
            get { return this; }
        }

        public bool AcceptTouchWith(Unit unit)
        {
            throw new NotImplementedException();
        }
    }
}
